define(['text!pages/cardtable/cardtable.html', 'css!pages/cardtable/cardtable', 'pages/cardtable/cardtablemeta', 'uuigrid'], function(html) {
    var init = function(element) {


        //元数据持久化方式
        //var listUrl = ctx+'/cardtablemeta/list';
        //var delUrl = ctx+'/cardtablemeta/del/';
        //var saveUrl = ctx+'/cardtablemeta/save';

        //mybtais持久化方式
        var listUrl = ctx + '/cardtable/list';
        var delUrl = ctx + '/cardtable/del/';
        var saveUrl = ctx + '/cardtable/save';
        var viewModel = {
            app: {},
            /* 数据模型 */
            draw: 1,
            totlePage: 0,
            pageSize: 5,
            totleCount: 0,
            dt1: new u.DataTable(metaCardTable),
            dtnew: new u.DataTable(metaCardTable),
            radiodatacontroller: [{
                value: 'Y',
                name: '是'
            }, {
                value: 'N',
                name: '否'
            }],
            radiodataassociate: [{
                value: 'Y',
                name: '是'
            }, {
                value: 'N',
                name: '否'
            }],
            combodatamodel: [{
                name: '信任',
                value: 'Y'
            }, {
                name: '不信任',
                value: 'N'
            }],

            event: {
                //清除datatable数据
                clearDt: function(dt) {
                    dt.removeAllRows();
                    dt.clear();
                },
                // 卡片表数据读取
                initCardTableList: function() {
                    var jsonData = {
                        pageIndex: viewModel.draw - 1,
                        pageSize: viewModel.pageSize,
                        sortField: "createtime",
                        sortDirection: "asc"
                    };
                    $(element).find("#search").each(function() {
                        if (this.value == undefined || this.value == '' || this.value.length == 0) {
                            //不执行操作
                        } else {
                            jsonData['search_searchParam'] = this.value.replace(/(^\s*)|(\s*$)/g, ""); //去掉空格
                        }
                    });
                    $.ajax({
                        type: 'get',
                        url: listUrl,
                        dataType: 'json',
                        data: jsonData,
                        contentType: 'application/json;charset=utf-8',
                        success: function(res) {
                            if (res) {
                                if (res.success == 'success') {
                                    if (res.detailMsg.data) {
                                        viewModel.totleCount = res.detailMsg.data.totalElements;
                                        viewModel.totlePage = res.detailMsg.data.totalPages;
                                        viewModel.event.comps.update({
                                            totalPages: viewModel.totlePage,
                                            pageSize: viewModel.pageSize,
                                            currentPage: viewModel.draw,
                                            totalCount: viewModel.totleCount
                                        });
                                        viewModel.dt1.removeAllRows();
                                        viewModel.dt1.clear();
                                        viewModel.dt1.setSimpleData(res.detailMsg.data.content, {
                                            unSelect: true
                                        });
                                    }
                                } else {
                                    var msg = "";
                                    if (res.success == 'fail_global') {
                                        msg = res.message;
                                    } else {
                                        for (var key in res.detailMsg) {
                                            msg += res.detailMsg[key] + "<br/>";
                                        }
                                    }
                                    u.messageDialog({
                                        msg: msg,
                                        title: '请求错误',
                                        btnText: '确定'
                                    });
                                }
                            } else {
                                u.messageDialog({
                                    msg: '后台返回数据格式有误，请联系管理员',
                                    title: '数据错误',
                                    btnText: '确定'
                                });
                            }
                        },
                        error: function(er) {
                            u.messageDialog({
                                msg: '请求超时',
                                title: '请求错误',
                                btnText: '确定'
                            });
                        }
                    });
                },
                //组装list
                genDataList: function(data) {
                    var datalist = [];
                    datalist.push(data);
                    return datalist;
                },
                //删除方法
                deleteData: function(data) {
                    var datalist = viewModel.event.genDataList(data);
                    var json = JSON.stringify(datalist);
                    $.ajax({
                        url: delUrl,
                        data: json,
                        dataType: 'json',
                        type: 'post',
                        contentType: 'application/json',
                        success: function(res) {
                            //md.close();
                            if (res) {
                                if (res.success == 'success') {
                                    u.messageDialog({
                                        msg: '删除成功',
                                        title: '操作提示',
                                        btnText: '确定'
                                    });
                                } else {
                                    var msg = "";
                                    for (var key in res.message) {
                                        msg += res.message[key] + "<br/>";
                                    }
                                    u.messageDialog({
                                        msg: 'msg',
                                        title: '操作提示',
                                        btnText: '确定'
                                    });
                                }
                            } else {
                                u.messageDialog({
                                    msg: '无返回数据',
                                    title: '操作提示',
                                    btnText: '确定'
                                });
                            }

                        },
                        error: function(er) {
                            u.messageDialog({
                                msg: '操作请求失败，' + er,
                                title: '操作提示',
                                btnText: '确定'
                            });
                        }
                    });
                },
                //新增和更新方法
                saveData: function(data) {
                    var datalist = viewModel.event.genDataList(data);
                    var json = JSON.stringify(datalist);
                    $.ajax({
                        url: saveUrl,
                        type: 'post',
                        data: json,
                        dataType: 'json',
                        contentType: 'application/json',
                        success: function(res) {
                            if (res) {
                                if (res.success == 'success') {
                                    viewModel.event.initCardTableList();
                                    u.messageDialog({
                                        msg: '操作成功',
                                        title: '操作提示',
                                        btnText: '确定'
                                    });
                                    md.close();
                                } else {
                                    var msg = "";
                                    if (res.success == 'fail_global') {
                                        msg = res.message;
                                    } else {
                                        for (var key in res.detailMsg) {
                                            msg += res.detailMsg[key] + "<br/>";
                                        }
                                    }
                                    u.messageDialog({
                                        msg: msg,
                                        title: '操作提示',
                                        btnText: '确定'
                                    });
                                }
                            } else {
                                u.messageDialog({
                                    msg: '没有返回数据',
                                    title: '操作提示',
                                    btnText: '确定'
                                });
                            }
                        }
                    });
                },
                //分页相关
                pageChange: function() {
                    viewModel.event.comps.on('pageChange', function(pageIndex) {
                        viewModel.draw = pageIndex + 1;
                        viewModel.event.initCardTableList();
                    });
                },
                sizeChange: function() {
                    viewModel.event.comps.on('sizeChange', function(arg) {
                        viewModel.pageSize = parseInt(arg);
                        viewModel.draw = 1;
                        viewModel.event.initCardTableList();
                    });
                },
                //页面初始化
                pageInit: function() {
                    $(element).html(html);
                    viewModel.app = u.createApp({
                        el: element,
                        model: viewModel
                    });

                    var paginationDiv = $(element).find('#pagination')[0];
                    this.comps = new u.pagination({
                        el: paginationDiv,
                        jumppage: true
                    });
                    this.initCardTableList();
                    viewModel.event.pageChange();
                    viewModel.event.sizeChange();

                    //回车搜索
                    $('.input_enter').keydown(function(e) {
                        if (e.keyCode == 13) {
                            $('#searchBtn').trigger('click');
                            u.stopEvent(e);
                        }
                    });
                },
                //页面按钮事件绑定
                /* 导航的三个按钮 编辑 添加 删除 */
                editClick: function() {
                    $('#editPage').find('.u-msg-title').html("编辑");
                    viewModel.event.clearDt(viewModel.dtnew);
                    var row = viewModel.dt1.getSelectedRows()[0];
                    if (row) {
                        viewModel.dtnew.setSimpleData(viewModel.dt1.getSimpleData({
                            type: 'select'
                        }));
                        $('#code').attr("readonly", "readonly");
                        window.md = u.dialog({
                            id: 'editDialog',
                            content: '#editPage',
                            hasCloseMenu: true
                        });
                    } else {
                        u.messageDialog({
                            msg: '请选择要编辑的数据！',
                            title: '操作提示',
                            btnText: '确定'
                        });
                    }
                },
                addClick: function() {
                    $('#editPage').find('.u-msg-title').html("新增");
                    viewModel.event.clearDt(viewModel.dtnew);
                    var newr = viewModel.dtnew.createEmptyRow();
                    newr.setValue("radiodatacontroller", "Y");
                    newr.setValue("radiodataassociate", "Y");
                    viewModel.dtnew.setRowSelect(newr);
                    $('#code').removeAttr("readOnly");
                    window.md = u.dialog({
                        id: 'addDialog',
                        content: '#editPage',
                        hasCloseMenu: true
                    });
                    $('#addDialog').css('width', '70%');
                },
                delClick: function() {
                    var row = viewModel.dt1.getSelectedRows()[0];
                    if (row) {
                        var data = viewModel.dt1.getSelectedRows()[0].getSimpleData()
                        u.confirmDialog({
                            msg: "是否删除" + data.name + "?",
                            title: "删除确认",
                            onOk: function() {
                                viewModel.event.deleteData(data);
                                viewModel.dt1.removeRow(viewModel.dt1.getSelectedIndexs());
                            },
                            onCancel: function() {}
                        });
                    } else {
                        u.messageDialog({
                            msg: '请选择要删除的数据！',
                            title: '操作提示',
                            btnText: '确定'
                        });
                    }
                },
                searchClick: function() {
                    viewModel.draw = 1;
                    viewModel.event.initCardTableList();
                },
                saveOkClick: function() {
                    var data = viewModel.dtnew.getSimpleData()[viewModel.dtnew.getSelectedIndexs()];
                    if (!viewModel.app.compsValidate($('#editPage')[0])) {
                        return;
                    }
                    viewModel.event.saveData(data);
                },
                saveCancelClick: function(e) {
                    md.close();
                }
            }
        }
        $(element).html(html);
        $('#display_none_input_focus').focus();
        viewModel.event.pageInit();
    }
    return {
        'template': html,
        init: init
    }
});
